package com.xykj.enterprise.wechat.ext.api.contacts.member;

import com.xykj.enterprise.wechat.bean.ext.contacts.member.UserListVo;
import com.xykj.enterprise.wechat.ext.service.contacts.ContactsUserService;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Function(description = "获取部门成员详情", parameters = {
        @Parameter(name = "access_token", type = Type.String, description = "access_token", required = true),
        @Parameter(name = "department_id", type = Type.Integer, description = "部门id", required = true),
        @Parameter(name = "fetch_child", type = Type.Integer, description = "是否递归获取成员", required = false),
})
@Component
public class UserList implements Action {

    @Autowired
    private ContactsUserService contactsUserService;

    @Override
    public Response execute(Request request) throws Exception {

        UserListVo userListVo = contactsUserService.userList(
                request.getString("access_token"),
                request.getInteger("department_id"),
                request.getInteger("fetch_child")
        );
        if (userListVo.getErrcode() != 0) {
            return Response.fail(userListVo.getErrmsg());
        }
        return Response.success().put("data", userListVo);
    }
}